package com.tanhua.mongo.api;

import com.tanhua.api.service.RecommendUserApi;
import com.tanhua.model.mongo.RecommendUser;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    public MongoTemplate mongoTemplate;

    // 获取最佳的佳人
    @Override
    public RecommendUser findWithMaxScore(Long toUserId) {

        // 根据当前用户查询评分第一的推荐人
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        Query query = new Query(criteria)
                .limit(1)
                .with(Sort.by(Sort.Order.desc("score")));

        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);

        // 判断是否有数据
        if (recommendUser == null) {
            // 没有最佳佳人，就获取评分最高的数据
            query = new Query()
                    .with(Sort.by(Sort.Order.desc("score")));

            recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        }

        return recommendUser;
    }

    // 获取所有的推荐人
    @Override
    public List<RecommendUser> findByUserIdList(Long toUserId) {

        Criteria criteria = Criteria.where("toUserId").is(toUserId);

        Query query = new Query(criteria);

        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);

        return recommendUserList;
    }

    @Override
    public RecommendUser findByUserIdAndToUserId(Long userId, Long toUserId) {
        // 查询推荐人数据
        RecommendUser recommendUser = mongoTemplate.findOne(new Query(Criteria
                .where("userId").is(userId)
                .and("toUserId").is(toUserId)), RecommendUser.class);

        // 判断推荐人是否为空
        if (recommendUser == null) {
            // 将当前用户设置成推荐人，姻缘值为95
            recommendUser = new RecommendUser();
            recommendUser.setUserId(userId);
            recommendUser.setToUserId(toUserId);
            recommendUser.setScore(95d);
            recommendUser.setDate(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd")));

            mongoTemplate.save(recommendUser);
        }

        return recommendUser;
    }

    @Override
    public List<RecommendUser> findByUserIds(Long userId, List<Long> likeUserIds, int count) {
        Criteria criteria = Criteria.where("toUserId").is(userId).and("userId").nin(likeUserIds);

        // 随机获取count条数据
        TypedAggregation<?> aggregation = Aggregation.newAggregation(
                RecommendUser.class,
                Aggregation.match(criteria),
                Aggregation.sample(count)
        );
        AggregationResults<RecommendUser> aggregate =
                mongoTemplate.aggregate(aggregation, RecommendUser.class);

        return aggregate.getMappedResults();
    }
}
